﻿using HSJM.Service.MWS.Infrastructure.Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace HSJM.Service.MWS.Infrastructure
{
    /// <summary>
    /// Repository接口
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IRepository<T> where T : class, IEntity
    {
        /// <summary>
        /// 获取事物
        /// </summary>
        /// <returns></returns>
        IDbTransaction GetTransaction();

        #region 查询

        /// <summary>
        /// 查询集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <param name="commandType"></param>
        /// <returns></returns>
        IEnumerable<T> GetList(string sql = null, object param = null, CommandType? commandType = null);

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <param name="commandType"></param>
        /// <returns></returns>
        Task<IEnumerable<T>> GetListAsync( object param = null, string sql = null, CommandType? commandType = null);

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        Task<IEnumerable<T>> GetListAsync(string sqlStr);

        /// <summary>
        /// 获取行数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        T Get(object id);

        /// <summary>
        ///  获取行数据（异步）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<T> GetAsync(object id);

        /// <summary>
        /// 分页方法
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="param">参数</param>
        /// <param name="transaction"></param>
        /// <param name="commandTimeout"></param>
        /// <returns></returns>
        PagedResult<T> GetPageList(int pageIndex, int pageSize, string sql = null, object param = null);
        #endregion

        #region  删除
        /// <summary>
        /// 删除（事物）
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="isDel">是否硬删除（默认软删除）</param>
        /// <param name="dbTransaction"></param>
        /// <returns></returns>
        bool Delete(T entity, bool isDel = false, IDbTransaction dbTransaction = null);

        /// <summary>
        /// 异步删除（事物）
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="isDel">是否硬删除（默认软删除）</param>
        /// <param name="dbTransaction"></param>
        /// <returns></returns>
        Task<bool> DeleteAsync(T entity, bool isDel = false, IDbTransaction dbTransaction = null);


        /// <summary>
        /// 删除表所有数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        Task<bool> DeleteAll();

        /// <summary>
        /// 异步删除表所有数据
        /// </summary>
        /// <returns></returns>
        Task<bool> DeleteAllAsync();
        #endregion

        #region 新增
        /// <summary>
        /// 添加行数据（事物）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        long Insert(T entity, IDbTransaction dbTransaction = null);


        /// <summary>
        /// 异步添加行数据（事物）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<int> InsertAsync(T entity, IDbTransaction dbTransaction = null);
        #endregion

        #region 修改

        /// <summary>
        /// 修改（事物）
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="dbTransaction"></param>
        /// <returns></returns>
        bool Update(T entity, IDbTransaction dbTransaction = null);

        /// <summary>
        /// 异步修改（事物）
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="dbTransaction"></param>
        /// <returns></returns>
        Task<bool> UpdateAsync(T entity, IDbTransaction dbTransaction = null);
        #endregion
    }
}
